-
-
Notifications
You must be signed in to change notification settings - Fork 821
feat: add C implementation for math/base/special/minmax
#6296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Coverage Report
The above coverage report was generated for the changes in this PR. |
* @param info callback data | ||
* @return Node-API value | ||
*/ | ||
static napi_value addon( napi_env env, napi_callback_info info ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have kept the addon logic as this for now and going through https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/napi to have an idea for a utility for void
functions.
for this file I have refactored this code-block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out we can convert it a very simplified fashion like:-
static napi_value addon( napi_env env, napi_callback_info info ) {
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, z, zlen, argv, 2 );
double min;
double max;
stdlib_base_minmax( x, y, &min, &max );
double *op = (double *)z;
op[ 0 ] = min;
op[ 1 ] = max;
return NULL;
}
just searching for an utility that can accumulate void
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something similar was also done in rempio2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Neerajpathak07 You can do something like this:
In addon.c
, you can have:
#include "stdlib/math/base/special/minmax.h"
#include "stdlib/napi/export.h"
#include "stdlib/napi/argv.h"
#include "stdlib/napi/argv_double.h"
#include "stdlib/napi/argv_float64array.h"
#include <node_api.h>
/**
* Receives JavaScript callback invocation data.
*
* @param env environment under which the function is invoked
* @param info callback data
* @return Node-API value
*/
static napi_value addon( napi_env env, napi_callback_info info ) {
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, out, outlen, argv, 2 );
stdlib_base_minmax( x, y, &out[ 0 ], &out[ 1 ] );
return NULL;
}
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
And, for native.js
:
function minmax( x, y ) {
var out = new Float64Array( 2 );
addon( x, y, out );
return [ out[ 0 ], out[ 1 ] ];
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gunjjoshi Bingo!!
I had drafted a similar logic earlier but was confused on how to go about the void function. But you just clarified it thank you soo much!!
@gunjjoshi LMK your opinion on this!! |
Not sure though why only C examples CI tests are failing for |
/stdlib merge |
@anandkaranubc, the slash command failed to complete. Please check the workflow logs for details. |
Signed-off-by: Karan Anand <[email protected]>
/stdlib merge |
#include <stdlib.h> | ||
|
||
/** | ||
* Evaluates the min and max of two values. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Evaluates the min and max of two values. | |
* Evaluates the minimum and maximum values. |
* @param x First value | ||
* @param y Second value | ||
* @param min destination to store minimum value | ||
* @param max destination to store maximum value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param max destination to store maximum value | |
* @param x first number | |
* @param y second number | |
* @param min destination to store the minimum value | |
* @param max destination to store the maximum value |
* @param max destination to store maximum value | ||
* | ||
* @example | ||
* #include <stdint.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no integers in the example. This can be removed
* double min; | ||
* double max; | ||
* stdlib_base_minmax( 3.14, NaN ); | ||
* // returns [ NaN, NaN ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No return. Function type is void.
#include "stdlib/math/base/special/minmax.h" | ||
#include "stdlib/math/base/assert/is_nan.h" | ||
#include "stdlib/math/base/assert/is_negative_zero.h" | ||
#include <stdlib.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed.
function minmax( x, y ) { | ||
var out = new Float64Array( 2 ); | ||
addon( x, y, out ); | ||
return [ out[ 0 ], out[ 1 ] ]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return [ out[ 0 ], out[ 1 ] ]; | |
return out; |
* | ||
* @example | ||
* var v = minmax( 3.14, 4.2 ); | ||
* // returns [ 3.14, 4.2 ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* // returns [ 3.14, 4.2 ] | |
* // returns <Float64Array>[ 3.14, 4.2 ] |
"@stdlib/napi/argv", | ||
"@stdlib/napi/argv-double", | ||
"@stdlib/napi/argv-float64array", | ||
"@stdlib/napi/export", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are not needed for examples/benchmark.
|
||
v = minmax( NaN, 3.14 ); | ||
t.strictEqual( isnan( v[ 0 ] ), true, 'returns NaN' ); | ||
t.strictEqual( isnan( v[ 1 ] ), true, 'returns NaN' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.strictEqual( isnan( v[ 1 ] ), true, 'returns NaN' ); | |
t.strictEqual( isnan( v[ 0 ] ), true, 'returns expected value' ); |
Applies here and below.
#endif | ||
|
||
/** | ||
* Returns the minimum and maximum values. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Returns the minimum and maximum values. | |
* Evaluates the minimum and maximum values. |
The function doesn't return anything.
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
|
||
for ( i = 0; i < 10; i++ ) { | ||
stdlib_base_minmax( x[ i ], y[ i ], &min, &max ); | ||
printf( "x: %lf => min: %lf, y: %lf, minmax(x, y): %lf\n", x[ i ], y[ i ], min, max ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf( "x: %lf => min: %lf, y: %lf, minmax(x, y): %lf\n", x[ i ], y[ i ], min, max ); | |
printf( "x: %lf, y: %lf => min: %lf, max: %lf\n", x[ i ], y[ i ], min, max ); |
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
#include "stdlib/math/base/assert/is_negative_zero.h" | ||
|
||
/** | ||
* Evaluates the minimum and maximum values in a single pass. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the reviewers: I've updated the description, but if "Evaluates the minimum and maximum values." is sufficiently clear on its own, I will revert the changes.
Signed-off-by: Karan Anand <[email protected]>
Signed-off-by: Karan Anand <[email protected]>
Progresses #649,
Resolves #2106.
Description
This pull request:
math/base/special/minmax
Related Issues
This pull request:
@stdlib/math/base/special/minmax
#2106Questions
No.
Other
No.
Checklist
@stdlib-js/reviewers